package com.bytecollege.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.bytecollege.model.*;
import com.bytecollege.service.FootmarkService;
import com.bytecollege.service.Impl.*;
import com.bytecollege.vo.CremarkVO;

import lombok.extern.java.Log;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.net.ssl.HttpsURLConnection;
import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;


/**
 * @author Mr.X
 */
@Log
@Controller
@RequestMapping("/course")
public class CourseController {
    private static final Integer LIMIT = 6;
    @Autowired
    private CourseServiceImpl courseService;
    @Autowired
    private CtypeServiceImpl ctypeService;
    @Autowired
    private FootmarkServiceImpl footmarkService;
    @Autowired
    private CourseCollectServiceImpl courseCollectService;
    @Autowired
    private CremarkServiceImpl cremarkService;

    /**
     * 查询所有课程
     * @param pageIndex 页面索引
     * @param model
     * @return 跳转页面
     */
    @GetMapping("/getAll")
    public String getAll(@RequestParam(value = "pageIndex",defaultValue = "1") Integer pageIndex, Model model, HttpSession session){
        Page<Course> page =new Page<>(pageIndex,3);
        model.addAttribute("pageIndex",pageIndex);
        List<Course> courseList = courseService.getAll(page);
        model.addAttribute("page",page);
        session.setAttribute("ctype",0);
        model.addAttribute("courseList",courseList);
        return "course/courseList";
    }

    /**
     * 查询单个课程和课程评论
     * 增加点击量
     * 增加足迹
     * @param courseId
     * @param model
     * @return
     */
    @GetMapping("/getById")
    @Transactional(rollbackFor = Exception.class)
    public String getById(Integer courseId,@RequestParam(value = "pageIndex",defaultValue = "1") Integer pageIndex,Model model,HttpSession session){
        User user = (User) session.getAttribute("data");
        if (user == null){
            return "redirect:/user/tologin";
        }
        Course course = courseService.getById(courseId);
        model.addAttribute("course",course);
        //点击量
        modClicks(course.getCourseId());
        //足迹
        Footmark footmark = Footmark.builder()
                .footmarkName(course.getCourseName())
                .userId(user.getUserId())
                .addtime(new Date())
                .build();
        List<Footmark> footmarkList = footmarkService.getAll();
        if (footmarkList.size() == 0){
            log.info(String.valueOf(0));
            addFootmark(footmark);
        }else {
            for(Footmark f : footmarkList){
                if (f.getFootmarkName().equals(footmark.getFootmarkName()) && f.getUserId().equals(footmark.getUserId())){
                    log.info(String.valueOf(1));
                    footmark.setFootmarkId(f.getFootmarkId());
                    footmarkService.modFootmark(footmark);
                    break;
                }
                else {
                    log.info(String.valueOf(0));
                    addFootmark(footmark);
                    break;
                }
            }
        }
        //分页
        Page<Cremark> page = new Page<>(pageIndex,LIMIT);
        List<Cremark> cremarkList = cremarkService.getCremark(page,courseId);
        //课程收藏量
        Integer collectNum = courseCollectService.getCollectNumByCourseId(courseId);
        Integer collectStatus = courseCollectService.getCollectStatus(courseId, user.getUserId());
        Integer cremarkNum = cremarkService.getCremarkNum();
        model.addAttribute("collectStatus",collectStatus);
        model.addAttribute("collectNum",collectNum);
        model.addAttribute("cremarkNum",cremarkNum);
        model.addAttribute("page",page);
        model.addAttribute("cremarkList",cremarkList);
        return "course/course";
    }

    /**
     * 添加课程评论
     * @param cremarkVO
     * @return 跳转页面
     */
    @PostMapping("/addCremark")
    public String addCremark(CremarkVO cremarkVO,HttpSession session){
        User user = (User) session.getAttribute("data");
        if (user == null){
            return "redirect:/user/tologin";
        }
        cremarkVO.setCremarkTime(new Date());
        cremarkVO.setUserId(user.getUserId());
        cremarkVO.setUserName(user.getUserName());
        Cremark cremark = new Cremark();
        BeanUtils.copyProperties(cremarkVO,cremark);
        cremarkService.addCremark(cremark);
        return "redirect:/course/getById?courseId="+cremark.getCourseId();
    }

    /**
     * 修改课程评论（Get)
     * 查询单个课程评论
     * @param cremarkId
     * @param model
     * @return 跳转页面
     */
    @GetMapping("/modCremark")
    public String modCremark(Integer cremarkId,Model model){
        Cremark cremark = cremarkService.getCremarkOne(cremarkId);
        model.addAttribute("cremark",cremark);
        return "course/course";
    }

    /**
     * 修改课程评论（Post)
     * @param cremark
     * @return 跳转页面
     */
    @PostMapping("/modCremark")
    public String modCremark(Cremark cremark){
        cremarkService.modCremark(cremark);
        return "course/course";
    }

    /**
     * 收藏课程
     * @param courseId
     * @param session
     * @return
     */
    @GetMapping("/addCollect")
    public String addCollect(Integer courseId,HttpSession session){
        User user = (User) session.getAttribute("data");
        if (user == null){
            return "redirect:/user/tologin";
        }
        CourseCollect courseCollect = CourseCollect.builder()
                .userId(user.getUserId())
                .addTime(new Date())
                .courseId(courseId)
                .build();
        courseCollectService.addCollect(courseCollect);
        return "redirect:/course/getById?courseId="+courseId;
    }

    /**
     * 足迹
     * @param footmark
     * @return
     */
    @GetMapping("/addFootmark")
    public int addFootmark(Footmark footmark){
        return footmarkService.addFootmark(footmark);
    }

    /**
     * 查询所有课程类别
     * @param model
     * @return 跳转页面
     */
    @GetMapping("/getCtype")
    public String getCtype(Model model){
        List<Ctype> list = ctypeService.getCtype();
        model.addAttribute("list",list);
        return "course/courseList";
    }

    /**
     * 点击量
     * @return
     */
    @GetMapping("/modClicks")
    public Integer modClicks(Integer courseId){
        Course course = courseService.getById(courseId);
        course.setCourseClicks(course.getCourseClicks()+1);
        return courseService.modClicks(course);
    }

    /**
     * 获取VIP课程
      * @return
     */
    @GetMapping("/getVIP")
    public String getVIP(@RequestParam(value = "pageIndex",defaultValue = "1") Integer pageIndex,Model model,HttpSession session){
        Page<Course> page = new Page<>(pageIndex,LIMIT);
        model.addAttribute("pageIndex",pageIndex);
        List<Course> courseList = courseService.getVIP(page);
        session.setAttribute("ctype",2);
        model.addAttribute("page",page);
        model.addAttribute("courseList",courseList);
        return "course/courseList";
    }

    /**
     * 查询免费课程
     * @param pageIndex
     * @param model
     * @return
     */
    @GetMapping("/getFree")
    public String getFree(@RequestParam(value = "pageIndex",defaultValue = "1") Integer pageIndex,Model model,HttpSession session){
        Page<Course> page = new Page<>(pageIndex, LIMIT);
        model.addAttribute("pageIndex",pageIndex);
        List<Course> courseList = courseService.getFree(page);
        session.setAttribute("ctype",1);
        model.addAttribute("page",page);
        model.addAttribute("courseList",courseList);
        return "course/courseList";
    }

    /**
     * 课程类别
     * @param pageIndex
     * @param cTypeName
     * @param model
     * @return
     */
    @GetMapping("/getByName")
    public String getByName(@RequestParam(value = "pageIndex",defaultValue = "1") Integer pageIndex,String cTypeName,Model model){
        Page<Course> page = new Page<>(pageIndex, LIMIT);
        Ctype ctype = ctypeService.getByName(cTypeName);
        List<Course> courseList = courseService.getByCtypeId(page, ctype.getCtypeId());
        model.addAttribute("cTypeName",cTypeName);
        model.addAttribute("courseList",courseList);
        return "course/courseList";
    }
}
